package com.wuwei.jy.controller;

import com.wuwei.dto.ComResult;
import com.wuwei.jy.Service.ArticleService;
import com.wuwei.jy.domain.g.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/6/6.
 */

@Controller
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private ArticleService articleService;

    private int limit = 20;


    @RequestMapping("/test")
    public String Test() {
        return "/home/test3";
    }


    @RequestMapping("/home")
    public String home(Model model) {
        List<Article> articleList = articleService.getArticleList((1 - 1) * limit,limit);
        model.addAttribute("articleList",articleList);
        model.addAttribute("page",1);
        long pageCount = articleService.getArticleListCount();
        if (pageCount % limit > 0)
            model.addAttribute("articleListCount",pageCount/limit + 1);
        else
            model.addAttribute("articleListCount",pageCount/limit);
        return "/home/home";
    }


    @ResponseBody
    @RequestMapping("/homeAjax")
    public ComResult<List<Article>> homeAjax(Model model) {
        List<Article> articleList = articleService.getArticleList((1 - 1) * limit,limit);
        model.addAttribute("articleList",articleList);
        model.addAttribute("page",1);
        long pageCount = articleService.getArticleListCount();
        if (pageCount % limit > 0)
            model.addAttribute("articleListCount",pageCount/limit + 1);
        else
            model.addAttribute("articleListCount",pageCount/limit);
        return new ComResult<List<Article>>(true,articleList,"");
    }


    @RequestMapping(value = "/articleInfo")
    public String articleInfo(int id,Model model) {
        Article article = articleService.articleInfo(id);
        model.addAttribute("article",article);
        if (article.getTemplet() != null && !article.getTemplet().equals("")){
            if (article.getTemplet().equals("")){
                return "/admin/" + article.getTemplet();
            }else{
                return "/admin/" + article.getTemplet();
            }
        } else {
            return "/admin/article_info1";
        }
    }


}
